# from flask_celery import Celery

from kombu import Exchange, Queue

from config import DevConfig
from celery import Celery, platforms

platforms.C_FORCE_ROOT = True

# 创建celery的实例
celery = Celery('test', broker=DevConfig.CELERY_BROKER_URL, backend=DevConfig.CELERY_BACKEND_URL)

celery.conf.update(
    celery_acks_late=False,  # 不允许重试
    celery_accept_content=['pickle', 'json'],
    celeryd_force_execv=True,  # 有些情况可以防止死锁
    celeryd_concurrency=25,  # 设置并发worker数量
    celeryd_max_tasks_per_child=100,  # 每个worker最多执行100个任务被销毁，可以防止内存泄漏
    broker_heartbeat=0,  # 心跳
    celeryd_task_limit=12 * 30,  # 超时时
    celery_queues=(
        Queue("add", Exchange("add"), routing_key="add"),
        Queue("show", Exchange("show"), routing_key="show")
    ),
    celery_routes={
        'tasks.add': {"queue": "add", "routing_key": "add"},
        'tasks.show': {"queue": "show", "routing_key": "show"}
    }
)
